import common.CommonFunction;
import common.Mask;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileReference;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

[Embed(source="/images/success.png")]private var successIcon:Class;
[Embed(source="/images/error.png")]private var errorIcon:Class;
[Embed(source="/images/question.png")]private var questionIcon:Class;

public var infoObject:Object;

public function onCreationComplete():void
{
	infoObject = new Object();
	infoObject["date"] = new Date();	
	getRoles.send(infoObject);
	Mask.show(CommonFunction.getMessages('pleaseWait'));
}

public var customerXML:XML;
public var customerList:ArrayCollection;
public function onSuccess(event:ResultEvent):void
{
	Mask.close();
	customerXML = event.result as XML;
	
	customerList = new ArrayCollection();
	for each(var obj:Object in customerXML.record)
	{
		customerList.addItem({select:true, code:obj.code, value:obj.description, grpCode:obj.grpCode});
	}
	emplyRole.dataProvider = customerList;
	emplyRole.labelField = "value";
}

public function onFailure(event:FaultEvent):void
{
	Mask.close();
	Alert.show(event.message + "");
}

public function clearAll():void
{
	FName.text = "";
	LName.text = "";
	emplyRole.selectedIndex = -1;
	gender.selectedIndex = -1;
	dob.text = "";
	address.text = "";
	country.selectedIndex = -1;
	state.selectedIndex = -1;
	city.selectedIndex = -1;
	phoneNumber.text = "";
	emailID.text = "";
//	imgPath.text = "";
}

protected function cancel_clickHandler(event:MouseEvent):void
{
	clearAll();
}

public var fileReference:FileReference = new FileReference();
public function init():void
{
	fileReference.addEventListener(Event.OPEN, callUpload);
}

public function callUpload(event:Event):void
{
	Alert.show(event.target.name + "");
//	imgPath.text = event.target.nativePath;
}

protected function img_path_clickHandler(event:MouseEvent):void
{
	fileReference.browse();	
}

protected function submit_clickHandler(event:MouseEvent):void
{
	infoObject = new Object();
	infoObject["date"] = new Date();
	infoObject["empName"] = FName.text + " " + LName.text;
	infoObject["roleId"] = emplyRole.selectedItem.code;
	infoObject["gender"] = gender.selectedLabel;
	infoObject["dob"] = dob.text;
	infoObject["add"] = address.text;
	infoObject["country"] = country.selectedLabel;
	infoObject["state"] = state.selectedLabel;
	infoObject["city"] = city.selectedLabel;
	infoObject["area"] = area.selectedLabel;
	infoObject["phNo"] = phoneNumber.text;
	infoObject["email"] = emailID.text;
//	infoObject["imgPath"] = imgPath.text;
	setEmployeeRole.send(infoObject);
}

protected function country_changeHandler(event:ListEvent):void
{
	state.dataProvider = groupDB.country.(@name == country.selectedLabel).state.@name;
}

protected function state_changeHandler(event:ListEvent):void
{
	city.dataProvider = groupDB.country.(@name == country.selectedLabel).state.(@name == state.selectedLabel).city;
}

protected function city_changeHandler(event:ListEvent):void
{
	area.dataProvider = groupDB.country.(@name == country.selectedLabel).state.(@name == state.selectedLabel).city.
		(@id == city.selectedItem.@id).area;
}

public function setSuccess(event:ResultEvent):void
{
	customerXML = event.result as XML;
	if(customerXML.booleanvalue == "true")
	{
		clearAll();
		phV.required = false;
		eV.required = false;
		fName.required = false;
		lName.required = false;
		add.required = false;
		Alert.show(CommonFunction.getMessages('reqSuccess'), "Success");
	}
	else
	{
		Alert.show(CommonFunction.getMessages('reqFailure'), "Failure");
	}
}